package com.qujia.device.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.qujia.common.utils.DateUtils;
import com.qujia.device.domain.TableDataInfoWithTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qujia.device.domain.DeviceSync;
import com.qujia.device.service.IDeviceSyncService;
import com.qujia.common.core.domain.AjaxResult;
import com.qujia.common.core.page.TableDataInfo;
import com.qujia.common.core.controller.BaseController;
import com.qujia.common.utils.poi.ExcelUtil;
/**
 * 双屏机同步Controller
 * 
 * @author joeley
 * @date 2022-12-23
 */
@RestController
@RequestMapping("/sync")
public class DeviceSyncController extends BaseController
{
    @Autowired
    private IDeviceSyncService deviceSyncService;

    /**
     * 查询双屏机同步列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DeviceSync deviceSync)
    {
        startPage();
        List<DeviceSync> list = deviceSyncService.selectDeviceSyncList(deviceSync);
        return getDataTable(list);
    }

    /**
     * 查询双屏机同步列表
     */
    @GetMapping("/dualScreen")
    public TableDataInfoWithTime lisWithTime(DeviceSync deviceSync)
    {
        TableDataInfoWithTime tableDataInfoWithTime = new TableDataInfoWithTime();

        // 没传createtime，首次查询，返回服务器时间
        if(deviceSync.getCreateTime() == null){
            tableDataInfoWithTime.setCreateTime(DateUtils.getNowDate());
            return tableDataInfoWithTime;
        }


        startPage();
        List<DeviceSync> list = deviceSyncService.selectDeviceSyncListWithCreatetimeOrder(deviceSync);
        TableDataInfo info = getDataTable(list);

        Date date = deviceSyncService.selectDeviceSyncListToGetMaxCreatetime(deviceSync);

        tableDataInfoWithTime.setTableDataInfo(info);
        tableDataInfoWithTime.setCreateTime(date);
        if(date == null){
            tableDataInfoWithTime.setCreateTime(deviceSync.getCreateTime());
        }

        return tableDataInfoWithTime;
    }


    /**
     * 导出双屏机同步列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeviceSync deviceSync)
    {
//        List<DeviceSync> list = deviceSyncService.selectDeviceSyncList(deviceSync);
//        ExcelUtil<DeviceSync> util = new ExcelUtil<DeviceSync>(DeviceSync.class);
//        util.exportExcel(response, list, "双屏机同步数据");
    }

    /**
     * 获取双屏机同步详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(deviceSyncService.selectDeviceSyncById(id));
    }

    /**
     * 新增双屏机同步
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody DeviceSync deviceSync)
    {
        return toAjax(deviceSyncService.insertDeviceSync(deviceSync));
    }

    /**
     * 修改双屏机同步
     */
    @PutMapping
    public AjaxResult edit(@RequestBody DeviceSync deviceSync)
    {
        return toAjax(deviceSyncService.updateDeviceSync(deviceSync));
    }

    /**
     * 删除双屏机同步
     */
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(deviceSyncService.deleteDeviceSyncByIds(ids));
    }
}
